<!--
 * @Descripttion: 熊大写的
 * @version: 1.0.0
 * @Author: xt_xiong
 * @Date: 2022-01-18 18:05:17
 * @LastEditors: xt_xiong
 * @LastEditTime: 2022-01-21 19:58:13
-->
<template>
  <div>
    <el-form
      ref="SubjectList"
      :model="SubjectList"
      :rules="rules"
      label-width="200"
    >
      <el-form-item v-if="!$route.query.name" prop="subjectID" label="所属学科">
        <el-select v-model="SubjectList.subjectID" style="width: 70%">
          <el-option
            v-for="item in subListMes"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item  label="目录名称" prop="directoryName">
        <el-input
          type="text"
          v-model="SubjectList.directoryName"
          style="width: 70%"
          placeholder="请输入目录名称"
        ></el-input>
      </el-form-item>
      <el-form-item style="margin-left: 240px">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects.js'
import { add, update } from '@/api/hmmm/directorys.js'
export default {
  name: '',
  props: {
    pid: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean
    },
    isEdit: {
      type: Boolean
    },
    // eslint-disable-next-line vue/require-default-prop
    row: {
      type: Object
    }
  },
  data () {
    return {
      subListMes: [],
      value: true,
      SubjectList: {
        subjectID: '',
        directoryName: ''
      },
      rules: {
        subjectID: [
          { required: true, message: '学科目录不能为空', trigger: 'blur' }
        ],
        directoryName: [
          { required: true, message: '所属目录不能为空', trigger: 'blur' },
          { min: 1, max: 8, message: '所属目录为1-8位', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getList()
  },
  mounted () {},
  methods: {
    // 获取简单列表
    async getList () {
      const { data: res } = await simple()
      this.subListMes = res
    },
    async submit () {
      this.$refs.SubjectList.validate((valid) => {
        if (!valid) return
        this.isEdit ? this.editSubListMes() : this.toAddSubListMes()
      })
      this.closeHandler()
    },
    // 学科信息回显(学科修改) ajax
    async editSubListMes () {
      await update(this.SubjectList)
      this.$message.success('修改成功')
      window.location.reload()
    },
    async toAddSubListMes () {
      if (this.$route.query.name) {
        this.SubjectList.subjectID = Number(this.$route.query.id)
      }
      // this.SubjectList.id = null
      await add(this.SubjectList)
      this.$message.success('添加成功')
      this.$emit('getSubList')
    },
    // 提交目录信息的ajax

    closeHandler () {
      this.$emit('close')
    },
    cancel () {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
el-input:focus::-webkit-input-placeholder {
  color: transparent;
}
/* Mozilla Firefox 4 to 18 */

el-input:focus:-moz-placeholder {
  color: transparent;
}

/* Mozilla Firefox 19+ */

el-input:focus::-moz-placeholder {
  color: transparent;
}

/* Internet Explorer 10+ */

el-input:focus:-ms-input-placeholder {
  color: transparent;
}
</style>
